#!/bin/bash
# https://code.google.com/p/measureit/issues/detail?id=59
#
### BEGIN INIT INFO
# Provides:          measureit
# Required-Start:    $remote_fs $mysql $syslog $all
# Required-Stop:     $remote_fs $syslog
# Should-Start:      $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Current Cost data grabber
# Description:       Starts the listening process on /dev/ttyUSB0 to grab sensor data from currentcost device
### END INIT INFO

. /lib/lsb/init-functions
SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
case "${1:-''}" in
  'start')
        # Need to determine if process is already running
        set +e
        r=`ps aux | grep python | grep data-input.py | wc -l`
        set -e
        if [ "$r" = "0" ] ; then
          log_daemon_msg "Starting CurrentCost monitor service" "measureit"
          /usr/bin/python /var/www/measureit/measureit_system_files/python/data-input.py &
          log_end_msg 0
        fi
        ;;
  'stop')
        r=`pgrep -f "/usr/bin/python /var/www/measureit/measureit_system_files/python/data-input.py"`
        kill -9 $r
        ;;
  'restart')
        $SELF stop
        $SELF start
        ;;
esac
